<?php

/**
 * This is the model class for table "{{job_request}}".
 *
 * The followings are the available columns in table '{{job_request}}':
 * @property integer $id
 * @property integer $author_id
 * @property integer $job_id
 * @property string $content
 */
class JobRequest extends CActiveRecord
{
    public $email;
    public $username;

	/**
	 * Returns the static model of the specified AR class.
	 * @return JobRequest the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{job_request}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
        if (!Yii::app()->user->isGuest) {
		    return array(
			    array('content', 'required'),
    			// The following rule is used by search().
	    		// Please remove those attributes that should not be searched.
		    	array('id, author_id, job_id, content', 'safe', 'on'=>'search'),
		    );
        } else {
            return array(
			    array('username, email, content', 'required'),
                array('username', 'length', 'max'=>'128'),
                array('email', 'email'),
    			// The following rule is used by search().
	    		// Please remove those attributes that should not be searched.
		    	array('id, author_id, job_id, content', 'safe', 'on'=>'search'),
		    );
        }
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'job' => array(self::BELONGS_TO, 'Job', 'job_id'),
            'author' => array(self::BELONGS_TO, 'User', 'author_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'author_id' => 'Author',
			'job_id' => 'Job',
			'content' => Yii::t('mj-base', 'Content'),
            'email' => Yii::t('mj-base', 'Email'),
            'username' => Yii::t('mj-base', 'Name'),
		);
	}

    public function getUrl($job=null)
	{
		if($job===null)
			$job=$this->job;
		return $job->url.'#c'.$this->id;
	}

    /**
	 * @return string the hyperlink display for the current comment's author
	 */
	public function getAuthorLink()
	{
		if(!empty($this->url))
			return CHtml::link(CHtml::encode($this->author->username),$this->author->profileUrl);
		else
			return CHtml::encode($this->author->username);
	}

    /**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord) {
                if (Yii::app()->user->isGuest) {
                    $this->author_id = User::getUser($this->email, $this->username)->id;
                } else {
                    $this->author_id = Yii::app()->user->id;
                }
				$this->create_time=time();
            }
			return true;
		}
		else
			return false;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('author_id',$this->author_id);
		$criteria->compare('job_id',$this->job_id);
		$criteria->compare('content',$this->content,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}
